<?php
	session_start();
	class VideosManager{
		public static function Add($nombre, $descripcion, $thumb, $path){
			$db = DataBase::getInstance();
			$db->execute("INSERT INTO videos(path, thumb, nombre,descripcion) VALUES ('".$path."','". $thumb."','".$nombre."','".$descripcion."')");
			$last_inserted_id = $db->last_inserted_id();

			FileManager::RenameFile($path, "vpath_".$last_inserted_id);
			FileManager::RenameFile($thumb, "vthumb_".$last_inserted_id);

			return VideosManager::Get($last_inserted_id);
		}
		
		public static function Update($id, $path, $thumb, $nombre,$descripcion){
			$db = DataBase::getInstance();

			if($path != "")
				$txt_path = ", path = '".$path."'";
			if($thumb != "")
				$txt_thumb = ", thumb = '".$thumb."'";

			FileManager::RenameFile($path, "vpath_".$id);
			FileManager::RenameFile($thumb, "vthumb_".$id);

			$db->execute("UPDATE videos SET 
				nombre = '".$nombre."'

				".$txt_path."
				".$txt_thumb."
				,descripcion = '".$descripcion."' 
				where id_video = ". $id);
		}
		
		public static function GetAll(){
			$db = DataBase::getInstance();
			$db->setQuery("select id_video id, path, thumb, nombre, descripcion from videos");
			$list = $db->loadObjectList();
			return $list;
		}
		
		public static function GetByOrganizacion($id){
			$db = DataBase::getInstance();
			$db->setQuery("select v.id_video id, v.path, v.thumb, v.nombre, v.descripcion from videos v 
			inner join videos_organizaciones vo on vo.id_video = v.id_video where vo.id_organizacion = ". $id);
			$list = $db->loadObjectList();
			return $list;
		}


		public static function SetByOrganizacion($id_video, $id_organizacion){
			$db = DataBase::getInstance();
			$db->execute("insert into videos_organizaciones (id_video, id_organizacion) VALUES (".$id_video.",".$id_organizacion.")");
		}

		public static function DeleteByOrganizacion($id){
			$db = DataBase::getInstance();
			$db->execute("delete from videos_organizaciones where id_organizacion = ".$id);
		}
		
		public static function Get($id){
			$db = DataBase::getInstance();
			$db->setQuery("select id_video id, path, thumb, nombre, descripcion from videos WHERE id_video= ".$id);
			$list = $db->loadObjectList();
			return $list[0];
		}

		public static function Delete($id){
			$db = DataBase::getInstance();
			$db->execute("DELETE FROM videos WHERE id_video = ".$id);
		}		
	}
?>